package com.lfg.leetcode.leetcode500;


import java.math.BigDecimal;

public class No670maximumSwap {
    public static void main(String[] args) {

        BigDecimal bd = new BigDecimal("0.0001");bd.add(new BigDecimal("0.9"));
//        System.out.println(maximumSwap(98368));
    }
    public static int maximumSwap(int num) {
        String numStr = num+"";
        char[] array = numStr.toCharArray();

        int idMax = array.length-1;
        int idSwap1 = idMax;
        int idSwap2 = idMax;

        for(int i=array.length-1;i>=0;i--){
            if(array[i] > array[idMax]){
                idMax = i;
                continue;
            }

            if(array[i] < array[idMax]){
                idSwap1 = i;
                idSwap2 = idMax;
            }
        }
        char tmp = array[idSwap1];
        array[idSwap1] = array[idSwap2];
        array[idSwap2] = tmp;

        return Integer.parseInt(new String(array));



    }

}



